<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Trustworthy #-}</span><span>
</span><span id="line-2"></span><span>
</span><span id="line-3"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-4"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-5"></span><span class="hs-comment">-- Module      :  Data.STRef</span><span>
</span><span id="line-6"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow 2001</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- License     :  BSD-style (see the file libraries/base/LICENSE)</span><span>
</span><span id="line-8"></span><span class="hs-comment">--</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- Stability   :  experimental</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Portability :  non-portable (uses Control.Monad.ST)</span><span>
</span><span id="line-12"></span><span class="hs-comment">--</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Mutable references in the (strict) ST monad.</span><span>
</span><span id="line-14"></span><span class="hs-comment">--</span><span>
</span><span id="line-15"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-16"></span><span>
</span><span id="line-17"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">Data.STRef</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-18"></span><span>        </span><span class="annot"><span class="hs-comment">-- * STRefs</span></span><span>
</span><span id="line-19"></span><span>        </span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier">STRef</span></a></span><span class="hs-special">,</span><span>          </span><span class="hs-comment">-- abstract</span><span>
</span><span id="line-20"></span><span>        </span><span class="annot"><a href="GHC.STRef.html#newSTRef"><span class="hs-identifier">newSTRef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-21"></span><span>        </span><span class="annot"><a href="GHC.STRef.html#readSTRef"><span class="hs-identifier">readSTRef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-22"></span><span>        </span><span class="annot"><a href="GHC.STRef.html#writeSTRef"><span class="hs-identifier">writeSTRef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-23"></span><span>        </span><span class="annot"><a href="Data.STRef.html#modifySTRef"><span class="hs-identifier">modifySTRef</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-24"></span><span>        </span><span class="annot"><a href="Data.STRef.html#modifySTRef%27"><span class="hs-identifier">modifySTRef'</span></a></span><span>
</span><span id="line-25"></span><span> </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-26"></span><span>
</span><span id="line-27"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.ST.html"><span class="hs-identifier">GHC.ST</span></a></span><span>
</span><span id="line-28"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.STRef.html"><span class="hs-identifier">GHC.STRef</span></a></span><span>
</span><span id="line-29"></span><span>
</span><span id="line-30"></span><span class="hs-comment">-- | Mutate the contents of an 'STRef'.</span><span>
</span><span id="line-31"></span><span class="hs-comment">--</span><span>
</span><span id="line-32"></span><span class="hs-comment">-- &gt;&gt;&gt; :{</span><span>
</span><span id="line-33"></span><span class="hs-comment">-- runST (do</span><span>
</span><span id="line-34"></span><span class="hs-comment">--     ref &lt;- newSTRef &quot;&quot;</span><span>
</span><span id="line-35"></span><span class="hs-comment">--     modifySTRef ref (const &quot;world&quot;)</span><span>
</span><span id="line-36"></span><span class="hs-comment">--     modifySTRef ref (++ &quot;!&quot;)</span><span>
</span><span id="line-37"></span><span class="hs-comment">--     modifySTRef ref (&quot;Hello, &quot; ++)</span><span>
</span><span id="line-38"></span><span class="hs-comment">--     readSTRef ref )</span><span>
</span><span id="line-39"></span><span class="hs-comment">-- :}</span><span>
</span><span id="line-40"></span><span class="hs-comment">-- &quot;Hello, world!&quot;</span><span>
</span><span id="line-41"></span><span class="hs-comment">--</span><span>
</span><span id="line-42"></span><span class="hs-comment">-- Be warned that 'modifySTRef' does not apply the function strictly.  This</span><span>
</span><span id="line-43"></span><span class="hs-comment">-- means if the program calls 'modifySTRef' many times, but seldom uses the</span><span>
</span><span id="line-44"></span><span class="hs-comment">-- value, thunks will pile up in memory resulting in a space leak.  This is a</span><span>
</span><span id="line-45"></span><span class="hs-comment">-- common mistake made when using an 'STRef' as a counter.  For example, the</span><span>
</span><span id="line-46"></span><span class="hs-comment">-- following will leak memory and may produce a stack overflow:</span><span>
</span><span id="line-47"></span><span class="hs-comment">--</span><span>
</span><span id="line-48"></span><span class="hs-comment">-- &gt;&gt;&gt; import Control.Monad (replicateM_)</span><span>
</span><span id="line-49"></span><span class="hs-comment">-- &gt;&gt;&gt; :{</span><span>
</span><span id="line-50"></span><span class="hs-comment">-- print (runST (do</span><span>
</span><span id="line-51"></span><span class="hs-comment">--     ref &lt;- newSTRef 0</span><span>
</span><span id="line-52"></span><span class="hs-comment">--     replicateM_ 1000 $ modifySTRef ref (+1)</span><span>
</span><span id="line-53"></span><span class="hs-comment">--     readSTRef ref ))</span><span>
</span><span id="line-54"></span><span class="hs-comment">-- :}</span><span>
</span><span id="line-55"></span><span class="hs-comment">-- 1000</span><span>
</span><span id="line-56"></span><span class="hs-comment">--</span><span>
</span><span id="line-57"></span><span class="hs-comment">-- To avoid this problem, use 'modifySTRef'' instead.</span><span>
</span><span id="line-58"></span><span id="local-6989586621679565657"><span id="local-6989586621679565658"><span class="annot"><a href="Data.STRef.html#modifySTRef"><span class="hs-identifier hs-type">modifySTRef</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier hs-type">STRef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679565658"><span class="hs-identifier hs-type">s</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679565657"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679565657"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679565657"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.ST.html#ST"><span class="hs-identifier hs-type">ST</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679565658"><span class="hs-identifier hs-type">s</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span></span><span>
</span><span id="line-59"></span><span id="modifySTRef"><span class="annot"><span class="annottext">modifySTRef :: forall s a. STRef s a -&gt; (a -&gt; a) -&gt; ST s ()
</span><a href="Data.STRef.html#modifySTRef"><span class="hs-identifier hs-var hs-var">modifySTRef</span></a></span></span><span> </span><span id="local-6989586621679565629"><span class="annot"><span class="annottext">STRef s a
</span><a href="#local-6989586621679565629"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679565628"><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679565628"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">STRef s a -&gt; a -&gt; ST s ()
forall s a. STRef s a -&gt; a -&gt; ST s ()
</span><a href="GHC.STRef.html#writeSTRef"><span class="hs-identifier hs-var">writeSTRef</span></a></span><span> </span><span class="annot"><span class="annottext">STRef s a
</span><a href="#local-6989586621679565629"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">(a -&gt; ST s ()) -&gt; (a -&gt; a) -&gt; a -&gt; ST s ()
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679565628"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">(a -&gt; ST s ()) -&gt; ST s a -&gt; ST s ()
forall (m :: * -&gt; *) a b. Monad m =&gt; (a -&gt; m b) -&gt; m a -&gt; m b
</span><a href="GHC.Base.html#%3D%3C%3C"><span class="hs-operator hs-var">=&lt;&lt;</span></a></span><span> </span><span class="annot"><span class="annottext">STRef s a -&gt; ST s a
forall s a. STRef s a -&gt; ST s a
</span><a href="GHC.STRef.html#readSTRef"><span class="hs-identifier hs-var">readSTRef</span></a></span><span> </span><span class="annot"><span class="annottext">STRef s a
</span><a href="#local-6989586621679565629"><span class="hs-identifier hs-var">ref</span></a></span><span>
</span><span id="line-60"></span><span>
</span><span id="line-61"></span><span class="hs-comment">-- | Strict version of 'modifySTRef'</span><span>
</span><span id="line-62"></span><span class="hs-comment">--</span><span>
</span><span id="line-63"></span><span class="hs-comment">-- @since 4.6.0.0</span><span>
</span><span id="line-64"></span><span id="local-6989586621679565624"><span id="local-6989586621679565625"><span class="annot"><a href="Data.STRef.html#modifySTRef%27"><span class="hs-identifier hs-type">modifySTRef'</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier hs-type">STRef</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679565625"><span class="hs-identifier hs-type">s</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679565624"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="#local-6989586621679565624"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679565624"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.ST.html#ST"><span class="hs-identifier hs-type">ST</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679565625"><span class="hs-identifier hs-type">s</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span></span><span>
</span><span id="line-65"></span><span id="modifySTRef%27"><span class="annot"><span class="annottext">modifySTRef' :: forall s a. STRef s a -&gt; (a -&gt; a) -&gt; ST s ()
</span><a href="Data.STRef.html#modifySTRef%27"><span class="hs-identifier hs-var hs-var">modifySTRef'</span></a></span></span><span> </span><span id="local-6989586621679565622"><span class="annot"><span class="annottext">STRef s a
</span><a href="#local-6989586621679565622"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span id="local-6989586621679565621"><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679565621"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-66"></span><span>    </span><span id="local-6989586621679565620"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679565620"><span class="hs-identifier hs-var">x</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">STRef s a -&gt; ST s a
forall s a. STRef s a -&gt; ST s a
</span><a href="GHC.STRef.html#readSTRef"><span class="hs-identifier hs-var">readSTRef</span></a></span><span> </span><span class="annot"><span class="annottext">STRef s a
</span><a href="#local-6989586621679565622"><span class="hs-identifier hs-var">ref</span></a></span><span>
</span><span id="line-67"></span><span>    </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679565619"><span class="annot"><span class="annottext">x' :: a
</span><a href="#local-6989586621679565619"><span class="hs-identifier hs-var hs-var">x'</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">a -&gt; a
</span><a href="#local-6989586621679565621"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679565620"><span class="hs-identifier hs-var">x</span></a></span><span>
</span><span id="line-68"></span><span>    </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679565619"><span class="hs-identifier hs-var">x'</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; ST s () -&gt; ST s ()
</span><a href="../../ghc-prim/src/GHC.Prim.html#seq"><span class="hs-operator hs-var">`seq`</span></a></span><span> </span><span class="annot"><span class="annottext">STRef s a -&gt; a -&gt; ST s ()
forall s a. STRef s a -&gt; a -&gt; ST s ()
</span><a href="GHC.STRef.html#writeSTRef"><span class="hs-identifier hs-var">writeSTRef</span></a></span><span> </span><span class="annot"><span class="annottext">STRef s a
</span><a href="#local-6989586621679565622"><span class="hs-identifier hs-var">ref</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679565619"><span class="hs-identifier hs-var">x'</span></a></span><span>
</span><span id="line-69"></span></pre></body></html>